using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using RaidLog.Engine;
using RaidLog.WebClient.ImportData;

namespace RaidLog.WebClient
{
    public partial class Database : Form
    {
        ImportData.ImportDataSrv _Server = null;

        public Database(ImportData.ImportDataSrv server)
        {
            _Server = server;
            InitializeComponent();
            try
            {
                RaidLog.WebClient.ImportData.RaidData _RaidData = _Server.GetRaids();
                this.RaidBindingSource.DataSource = _RaidData;
            }
            catch (System.Web.Services.Protocols.SoapException ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.ToString());
                buttonInitializeDatabase.Enabled = true;
            }
            this.Text = server.Url;
        }

        private int GetSelectedRaidID()
        {
            return GetSelectedRaid().RaidID;
        }

        private RaidLog.WebClient.ImportData.RaidData.RaidRow GetSelectedRaid()
        {
            return ((RaidLog.WebClient.ImportData.RaidData.RaidRow)
                (((System.Data.DataRowView)(this.RaidDataGridView.SelectedRows[0].DataBoundItem)).Row));
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            RaidLog.WebClient.ImportData.RaidData _RaidData = _Server.GetRaids();
            this.RaidBindingSource.DataSource = _RaidData;
        }

        private void buttonCreateRaid_Click(object sender, EventArgs e)
        {
            CreateRaid _cRaid = new CreateRaid(_Server);
            _cRaid.Show();
        }

        private void button_ImportData_Click(object sender, EventArgs e)
        {
            ImportDataForm _cRaid = new ImportDataForm(_Server, GetSelectedRaid());
            _cRaid.Show();

        }

        private void buttonView_Click(object sender, EventArgs e)
        {
            LootData data = _Server.GetRaidLootData(GetSelectedRaid().RaidID);
            ViewRaid viewRaidForm = new ViewRaid(data);
            viewRaidForm.ShowDialog();
        }

        private void button_RecalcMeters_Click(object sender, EventArgs e)
        {
            _Server.RegenerateAnalysisData(GetSelectedRaid().RaidID);
            MessageBox.Show("Done!");
        }

        private void buttonInitializeDatabase_Click(object sender, EventArgs e)
        {
            _Server.InitializeDatabase();
            MessageBox.Show("Done!");
        }
    }
}